﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.popups.visitfriend {
    import reprise.ui.*;
    import flash.events.*;
    import reprise.external.*;
    import reprise.events.*;
    import reprise.controls.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.popups.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.external.*;

    public class VisitFriendPopupMediator extends AbstractPopupMediator {

        public var languageModel:LanguageModel;
        public var friendsModel:FriendsModel;
        private var _buttonContainer:UIComponent;
        private var _askButton:LabelButton;
        private var _okButton:LabelButton;
        private var _userData:Object;

        private static const BUTTON_MARGIN:int = 10;
        private static const ICON_WIDTH:int = 35;
        private static const ITEM_MARGIN:int = 5;

        override public function onRegister():void{
            var _local3:Object;
            var _local4:String;
            super.onRegister();
            var _local1:UIComponent = RepriseUtils.addComponent(this.view, "content");
            if (((((this.view.data) && (this.view.data.friend))) && ((this.friendsModel.friends.length > 0)))){
                _local3 = this.view.data.friend;
                _local4 = language.getTextByKey("visitfriend_reward_info", {friends_name:_local3.first_name});
                RepriseUtils.addLabel(_local1, "myriadProFontNormal headline", _local4);
                this.createFriendPicture(_local1, _local3);
                this._userData = this.view.data.userdata;
            } else {
                close();
            };
            RepriseUtils.addComponent(this.view, "bear");
            var _local2:UIComponent = RepriseUtils.addComponent(_local1, "itemContainer");
            RepriseUtils.validateComponent(_local2, this.onItemContainer);
            this.createIcons(_local2);
            this._buttonContainer = RepriseUtils.addComponent(this.view, "buttonContainer");
            this._buttonContainer.addEventListener(DisplayEvent.VALIDATION_COMPLETE, this.onButtonContainer);
            addOkButton("global_button_ok", this._buttonContainer);
        }
        private function createFriendPicture(_arg1:UIComponent, _arg2:Object):void{
            var _local3:UIComponent = RepriseUtils.addComponent(_arg1, "friendPicFrame");
            var _local4:String = ((_arg2.picture) || (GameConstants.FACEBOOK_DEFAULT_PIC));
            var _local5:FacebookImageResource = new FacebookImageResource(_local4);
            _local5.addEventListener(Event.COMPLETE, this.imageResourceComplete);
            _local5.execute();
            RepriseUtils.addClipComponent(_local3, "image", _local5.loader());
        }
        private function imageResourceComplete(_arg1:Event):void{
            var _local2:ImageResource = ImageResource(_arg1.target);
            _local2.removeEventListener(Event.COMPLETE, this.imageResourceComplete);
        }
        private function onItemContainer(_arg1:UIComponent):void{
            var _local2:Array;
            var _local3:Number;
            if (((this.view.data) && (this.view.data.items))){
                _local2 = this.view.data.items;
                _local3 = (_local2.length * (ICON_WIDTH + ITEM_MARGIN));
                _arg1.setStyle("height", String(_local3));
            };
        }
        private function createIcons(_arg1:UIComponent):void{
            var _local2:Array;
            var _local3:Object;
            var _local4:UIComponent;
            var _local5:UIComponent;
            var _local6:String;
            if (((this.view.data) && (this.view.data.items))){
                _local2 = this.view.data.items;
                for each (_local3 in _local2) {
                    _local4 = RepriseUtils.addComponent(_arg1, "item");
                    _local5 = RepriseUtils.addComponent(_local4, (_local3.type + "Icon icon"));
                    RepriseUtils.validateComponent(_local5, this.onIconAdded);
                    _local6 = this.languageModel.getTextByKey(("global_amount_" + _local3.type), {amount:_local3.amount});
                    RepriseUtils.addLabel(_local4, "myriadProFontNormal itemLabel", _local6);
                };
            };
        }
        private function onIconAdded(_arg1:UIComponent):void{
            Displays.correctScaling(_arg1, _arg1, ICON_WIDTH, ICON_WIDTH, -1, 0);
            _arg1.setStyle("left", String((((ICON_WIDTH - _arg1.borderBoxWidth) * _arg1.scaleX) / 2)));
        }
        private function onButtonContainer(_arg1:DisplayEvent):void{
            this._buttonContainer.removeEventListener(DisplayEvent.VALIDATION_COMPLETE, this.onButtonContainer);
            var _local2:Number = 0;
            _local2 = (_local2 + (this._okButton) ? (this._okButton.borderBoxWidth + BUTTON_MARGIN) : 0);
            _local2 = (_local2 + (this._askButton) ? (this._askButton.borderBoxWidth + BUTTON_MARGIN) : 0);
            this._buttonContainer.setStyle("width", String(_local2));
        }
        override protected function createOkButton(_arg1:String, _arg2:UIComponent=null):LabelButton{
            this._okButton = createButton(_arg2, "greenButton", _arg1, "myriadProFont");
            return (this._okButton);
        }
        private function get view():VisitFriendPopupView{
            return ((viewComponent as VisitFriendPopupView));
        }
        override protected function onClose(_arg1:MouseEvent):void{
            dispatch(new InfoEvent(EventConstants.UPDATE_USER_DATA, null, this._userData));
            close();
        }
        override protected function onOk(_arg1:MouseEvent):void{
            dispatch(new InfoEvent(EventConstants.UPDATE_USER_DATA, null, this._userData));
            close();
        }

    }
}//package net.wooga.pets.views.popups.visitfriend 
